/*
 * PanelCanny.java
 *
 * Created on 2008年4月11日, 下午8:38
 */

package GUI;

import Global.*;
import Manager.Manager;

/**
 *
 * @author  littlechild
 */
public class PanelCanny extends javax.swing.JPanel {
    
    Manager manager;
    PanelCavas drawcavas;
    boolean isInitiating;
    static int maxSliderAngle=9;
    static int minSliderAngle=0;
    static double scaleAngle=Math.PI/32;
    /** Creates new form PanelCanny */
    public PanelCanny(Manager mg,PanelCavas pc) {
        manager=mg;
        drawcavas=pc;
        initComponents();
        init();
    }

    private void init() {
        isInitiating=true;
        this.jSliderHigh.setValue(30);
        this.jSliderLow.setValue(15);
        this.jSliderAngle.setMaximum(maxSliderAngle);
        this.jSliderAngle.setMinimum(minSliderAngle);
        this.jSliderAngle.setValue(4);
        isInitiating=false;
        preview();
    }
    private double sliderAngle2realAngle(int a) {
        return a*scaleAngle;
    }
    private void preview() {
        if(!isInitiating) {
            int mode;
            if(this.jCheckBox1.isSelected()) {
                mode=GlobalCanny.modeTracingEdge;
            }else {
                mode=GlobalCanny.modeNeighbour;
            }
            String paras[]=new String [] {
              String.valueOf(this.jSliderHigh.getValue()),
              String.valueOf(this.jSliderLow.getValue()),
              String.valueOf(mode),
              String.valueOf(sliderAngle2realAngle(this.jSliderAngle.getValue()))
            };
            this.drawcavas.setImage(
                    manager.operate(Global.opCanny, paras,true));
        }
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSliderHigh = new javax.swing.JSlider();
        jSliderLow = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabelHigh = new javax.swing.JLabel();
        jLabelLow = new javax.swing.JLabel();
        jButtonOk = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        jSliderAngle = new javax.swing.JSlider();
        jLabelAngle = new javax.swing.JLabel();

        jSliderHigh.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderHighStateChanged(evt);
            }
        });

        jSliderLow.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderLowStateChanged(evt);
            }
        });

        jLabel1.setText("高阈值：");

        jLabel2.setText("低阈值：");

        jButtonOk.setText("确定");
        jButtonOk.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOkActionPerformed(evt);
            }
        });

        jButtonCancel.setText("取消");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        jCheckBox1.setText("边界追踪模式");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jLabel3.setText("夹角判断：");

        jSliderAngle.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderAngleStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(jSliderHigh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel1)
                                .addComponent(jLabel2)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelLow)
                            .addComponent(jLabelHigh)
                            .addComponent(jLabelAngle)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(100, 100, 100)
                        .addComponent(jCheckBox1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jButtonOk)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jButtonCancel))
                            .addComponent(jSliderLow, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(jSliderAngle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(54, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addComponent(jCheckBox1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabelHigh))
                .addGap(18, 18, 18)
                .addComponent(jSliderHigh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(jLabelLow))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)))
                .addGap(18, 18, 18)
                .addComponent(jSliderLow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabelAngle))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderAngle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOk)
                    .addComponent(jButtonCancel))
                .addContainerGap(47, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jSliderLowStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderLowStateChanged
        // TODO add your handling code here:
        this.jLabelLow.setText(String.valueOf(this.jSliderLow.getValue()));
        preview();
    }//GEN-LAST:event_jSliderLowStateChanged

    private void jSliderHighStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderHighStateChanged
        // TODO add your handling code here:
        this.jLabelHigh.setText(String.valueOf(this.jSliderHigh.getValue()));
        preview();
    }//GEN-LAST:event_jSliderHighStateChanged

    private void jButtonOkActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOkActionPerformed
        // TODO add your handling code here:
        int mode;
        if(this.jCheckBox1.isSelected()) {
            mode=GlobalCanny.modeTracingEdge;
        }else {
            mode=GlobalCanny.modeNeighbour;
        }
        String paras[]=new String [] {
          String.valueOf(this.jSliderHigh.getValue()),
          String.valueOf(this.jSliderLow.getValue()),
          String.valueOf(mode),
          String.valueOf(sliderAngle2realAngle(this.jSliderAngle.getValue()))
        };
        this.drawcavas.setImage(
                manager.operate(Global.opCanny, paras));
        CommonFunc.disposeFrame(this);
    }//GEN-LAST:event_jButtonOkActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // TODO add your handling code here:
        this.drawcavas.setImage(manager.operate(Global.opNone, null));
        CommonFunc.disposeFrame(this);
    }//GEN-LAST:event_jButtonCancelActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        // TODO add your handling code here:
        preview();
    }//GEN-LAST:event_jCheckBox1ActionPerformed

    private void jSliderAngleStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jSliderAngleStateChanged
        // TODO add your handling code here:
        this.jLabelAngle.setText(
                String.valueOf(this.jSliderAngle.getValue())+"*PI/32");
        preview();
    }//GEN-LAST:event_jSliderAngleStateChanged
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOk;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabelAngle;
    private javax.swing.JLabel jLabelHigh;
    private javax.swing.JLabel jLabelLow;
    private javax.swing.JSlider jSliderAngle;
    private javax.swing.JSlider jSliderHigh;
    private javax.swing.JSlider jSliderLow;
    // End of variables declaration//GEN-END:variables
    
}
